package com.eboy.htmlviewer;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
	private EditText edtUrl = null;
	private EditText edtContent = null;
	Button btnOpen = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        edtUrl = (EditText)this.findViewById(R.id.editText1);
        edtContent = (EditText)this.findViewById(R.id.editText2);
        btnOpen = (Button) findViewById(R.id.button1);
        btnOpen.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				
				String imageUrl = edtUrl.getText().toString();
				try {
					URL url = new URL(imageUrl);
					HttpURLConnection  conn = (HttpURLConnection)url.openConnection();
					conn.setConnectTimeout(5000);
					conn.setRequestMethod("GET");
					if (conn.getResponseCode() == 200){
						InputStream inputStream = conn.getInputStream();
						ByteArrayOutputStream memStream = new ByteArrayOutputStream();
						byte[] buff = new byte[1024];
						int len = 0;
						while (( len = inputStream.read(buff)) != -1) {
							memStream.write(buff, 0, len);
						}
						String HtmlStr = new String(memStream.toByteArray(), "GB2312");
						edtContent.setText(HtmlStr);
						inputStream.close();
						memStream.close();
					}
					
				} catch (Exception e) {
					
					e.printStackTrace();
				}				
			}
        }); 
    }     

   
}
